package com.example.administrator.siku.menu.beans.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

/**
 * Created by Administrator on 2016/12/6.
 */
public class PullToRefreshRecycleView extends PullToRefreshBase<RecyclerView>{
    public PullToRefreshRecycleView(Context context) {
        super(context);
    }

    public PullToRefreshRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        RecyclerView recyclerView = new RecyclerView(context,attrs);
        return recyclerView;
    }

    @Override
    protected boolean isReadyForPullEnd() {
        RecyclerView recyclerView = getRefreshableView();
       int childCount = recyclerView.getChildCount();
        View child = recyclerView.getChildAt(childCount-1);
        int height =recyclerView.getHeight();
        int paddingBottom = recyclerView.getPaddingBottom();
        MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
        int bottomMagin = params.bottomMargin;
        int bottom = child.getBottom();
        int itemCount = recyclerView.getAdapter().getItemCount();
        int childAdapterPos = recyclerView.getChildAdapterPosition(child);
        return height == bottom+bottomMagin+paddingBottom&&childAdapterPos+1==itemCount;
    }

    @Override
    protected boolean isReadyForPullStart() {
        RecyclerView recyclerView = getRefreshableView();
        View child = recyclerView.getChildAt(0);
        int top = child.getTop();
        int paddingTop = recyclerView.getPaddingTop();
        MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
        int topMargin = params.topMargin;
        return top==topMargin+paddingTop;
    }
}
